package com.gitee.ting723.contacts;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class ContactRepository {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public List<Contact> findAll() {
        return jdbcTemplate.query(
                "select id, firstName , lastName, phoneNumber, emailAddress " +
                        " from contacts order by lastName",
                (rs, rowNum) -> new Contact(rs.getLong(1),
                        rs.getString(2)
                        , rs.getString(3)
                        , rs.getString(4)
                        , rs.getString(5)));
    }

    public void save(Contact contact) {
        jdbcTemplate.update(
                "insert into contacts " +
                        " (firstName , lastName , phoneNumber, emailAddress) " +
                        " values ( ?, ? ,? ,? ) ",
                contact.getFirstName(), contact.getLastName(),
                contact.getPhoneNumber(), contact.getEmailAddress()
        );
    }
}
